package org.leviatan.dataharbour.gui.swingcomponents;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JToolBar;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

import org.leviatan.dataharbour.gui.RuntimeContext;
import org.leviatan.dataharbour.gui.listeners.DataSourceTreeListener;
import org.leviatan.dataharbour.gui.swingcomponents.frames.AddNewDataSourceFrame;
import org.leviatan.dataharbour.gui.swingcomponents.frames.EditDataSourceFrame;
import org.leviatan.dataharbour.gui.swingcomponents.views.DataSourcesDefsPanel;
import org.leviatan.dataharbour.gui.swingcomponents.views.DataSourcesPersistenceResourcesPanel;
import org.leviatan.dataharbour.gui.swingmodels.TreeMdlDataSource;
import org.leviatan.dataharbour.gui.swingmodels.TreeNodeMdlDataSource;

public class DataSourcesTab extends JPanel {

	private static final long serialVersionUID = -1010166724017438248L;

	private JSplitPane mainSplitPane;
	
	private JSplitPane rightSplitPane;
	
	private JScrollPane jScrollPaneLeft;
	
	private JScrollPane persistenceResourcesScrollPane;
	
	private JScrollPane defsScrollPane;
	
	private JPanel persistenceResourcesPanel, defsPanel;
	
	private JTree dataSourceTree;

	/**
	 * This is the default constructor
	 */
	public DataSourcesTab() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(300, 200);
		this.setLayout(new BorderLayout());
		this.add(getMainJSplitPane());
		
		this.dataSourceTree.addTreeSelectionListener(
				new DataSourceTreeListener(dataSourceTree));
	}
	
	private JSplitPane getMainJSplitPane() {
		if (mainSplitPane == null) {
			mainSplitPane = new JSplitPane();
			mainSplitPane.setLeftComponent(getLeftJScrollPane());
			mainSplitPane.setRightComponent(getRightSplitPane());
		}
		return mainSplitPane;
	}
	
	private JSplitPane getRightSplitPane() {
		if (this.rightSplitPane == null) {
			this.rightSplitPane = new JSplitPane();
			rightSplitPane.setLeftComponent(getDefsScrollPane());
			rightSplitPane.setRightComponent(getPersistenceResourcesScrollPane());
		}
		return this.rightSplitPane;
	}
	
	private JScrollPane getLeftJScrollPane() {
		if (jScrollPaneLeft == null) {
			jScrollPaneLeft = new JScrollPane(getLeftPanel());
			jScrollPaneLeft.setPreferredSize(new Dimension(300, 500));
		}
		return jScrollPaneLeft;
	}
	
	private JScrollPane getPersistenceResourcesScrollPane() {
		if (this.persistenceResourcesScrollPane == null) {
			this.persistenceResourcesScrollPane = new JScrollPane(this.getPersistenceResourcesPanel());
			this.persistenceResourcesScrollPane.setPreferredSize(new Dimension(200,200));
		}
		return this.persistenceResourcesScrollPane;
	}
	
	private JScrollPane getDefsScrollPane() {
		if (this.defsScrollPane == null) {
			this.defsScrollPane = new JScrollPane(this.getDefsPanel());
			this.defsScrollPane.setPreferredSize(new Dimension(300,200));
		}
		return this.defsScrollPane;
	}
	
	private JPanel getDefsPanel() {
		if (this.defsPanel == null) {
			this.defsPanel = new DataSourcesDefsPanel();	
		}
		return this.defsPanel;
	}
	
	private JPanel getPersistenceResourcesPanel() {
		if (this.persistenceResourcesPanel == null) {
			this.persistenceResourcesPanel = new DataSourcesPersistenceResourcesPanel();
		}
		return this.persistenceResourcesPanel;
	}
	
	
	///////////////////////////////////////////////////////////////////////////
	
	private JPanel leftPanel;
	
	
	public JPanel getLeftPanel() {
	
		if (leftPanel == null) {
			leftPanel = new JPanel();
		}
		leftPanel.setLayout(new GridBagLayout());
		
		//this.setPreferredSize(size);
		
		this.dataSourceTree = this.initJTree();
		RuntimeContext.dataSourceTree = this.dataSourceTree;
		
		GridBagConstraints tBarConstr = new GridBagConstraints();
		tBarConstr.anchor = GridBagConstraints.NORTHWEST;
		tBarConstr.fill = GridBagConstraints.BOTH;
		tBarConstr.gridx = 0;
		tBarConstr.gridy = 0;
		
		GridBagConstraints treeConstr = new GridBagConstraints();
		treeConstr.anchor = GridBagConstraints.NORTHWEST;
		treeConstr.insets = new Insets(5, 5, 5, 5);
		treeConstr.fill = GridBagConstraints.BOTH;
		treeConstr.gridx = 0;
		treeConstr.gridy = 1;
		treeConstr.weightx = 1.0;
		treeConstr.weighty = 1.0;
		
		leftPanel.add(this.getToolBar(), tBarConstr);
		leftPanel.add(this.dataSourceTree, treeConstr);
		
		// add listeners to buttons
			
		return leftPanel;
	}
	
	private JTree initJTree() {
		if (dataSourceTree == null ) {
			dataSourceTree = new JTree();
			dataSourceTree.setModel(new TreeMdlDataSource());
			//ListenerFactory.initTreeSelectionListener(requestsTree);
			//jTree.setPreferredSize(size);
		}
		return dataSourceTree;
	}

	

	//	 tool bar
	///////////////////////////////////////////////////////////////////////////
	
	private JToolBar getToolBar() {
		
		JToolBar tBar = new JToolBar();
		
		JButton butAdd = null;
		JButton butDelete = null;
		JButton butEdit = null;
		
		tBar.setLayout(new GridBagLayout());
		
		GridBagConstraints butAddC = new GridBagConstraints();
		butAddC.anchor = GridBagConstraints.WEST;
		butAddC.insets = new Insets(0, 5, 0, 5);
		butAddC.gridx = 0;
		butAddC.gridy = 0;
		butAddC.fill = GridBagConstraints.BOTH;
	
		GridBagConstraints butDeleteC = new GridBagConstraints();
		butDeleteC.anchor = GridBagConstraints.WEST;
		butDeleteC.insets = new Insets(0, 0, 0, 5);
		butDeleteC.gridx = 1;
		butDeleteC.gridy = 0;
		
		GridBagConstraints butEditC = new GridBagConstraints();
		butEditC.anchor = GridBagConstraints.WEST;
		butEditC.insets = new Insets(0, 0, 0, 5);
		butEditC.gridx = 2;
		butEditC.gridy = 0;
		butEditC.weightx = 1.0;
		butEditC.weighty = 1.0;
		
		
		butAdd = new JButton("New");
		butDelete = new JButton("Remove");
		butEdit = new JButton("Edit");
		
		butAdd.addMouseListener(new MouseAdapter() {
			
			public void mouseClicked(MouseEvent e) {
				JFrame addFrame = new AddNewDataSourceFrame();
				
				addFrame.setLocationRelativeTo(DataSourcesTab.this);
				
				RuntimeContext.addDataSourceFrame = addFrame;
				addFrame.setAlwaysOnTop(true);
				addFrame.setVisible(true);
			}
		});
		
		butDelete.addMouseListener(new MouseAdapter() {
			
			public void mouseClicked(MouseEvent e) {
				String dsId = DataSourcesTab.this.getSelectedDSId();
				if (dsId != null) {
					RuntimeContext.dataHarbourService.removeDataSource(dsId);
					((TreeMdlDataSource) RuntimeContext.dataSourceTree.getModel()).reload();
					RuntimeContext.dataHarbourService.saveDataSources();
				}	
			}
		});
		
		butEdit.addMouseListener(new MouseAdapter() {
			
			public void mouseClicked(MouseEvent e) {
				String dsId = DataSourcesTab.this.getSelectedDSId();
				if (dsId == null)
					return;
				JFrame editF = new EditDataSourceFrame();
				RuntimeContext.editDataSourceFrame = editF;
				editF.setLocationRelativeTo(DataSourcesTab.this);
				editF.setVisible(true);
			}
			
		});
		
		tBar.add(butAdd, butAddC);
		tBar.add(butDelete, butDeleteC);
		tBar.add(butEdit, butEditC);
		
		tBar.setRollover(true);
		tBar.setFloatable(false);
		
		return tBar;
		
	}
	
	private String getSelectedDSId() {
		DefaultMutableTreeNode selectedDSNode = (DefaultMutableTreeNode) 
			RuntimeContext.dataSourceTree.getLastSelectedPathComponent();

		if (selectedDSNode == null) return null;

		TreeNodeMdlDataSource nodeModelDS = (TreeNodeMdlDataSource) 
			selectedDSNode.getUserObject();
		
		return nodeModelDS.getId();
	}
}
